//
//  TrainingManager.m
//  Pennyworth
//
//  Created by Chris Karr on 12/31/07.
//  Copyright 2007 Chris J. Karr. All rights reserved.
//

#import "TrainingManager.h"
#import "PredictionManager.h"

#define TRAINING_INTERVAL @"Training Interval"

#define AUDIBLE_CUE @"Audible Cue"
#define DEFAULT_INTERVAL 60.0

@implementation TrainingManager

// @synthesize trainingStatus;
// @synthesize trainingItemLabel;

- (void) awakeFromNib;
{
	timer = nil;
	// self.trainingStatus = @"Not Training";
	// self.trainingItemLabel = @"Start Training";
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	// [defaults addObserver:self forKeyPath:TRAINING_INTERVAL options:NSKeyValueObservingOptionNew context:NULL];
	[defaults setBool:NO forKey:IS_TRAINING];
	[defaults setBool:YES forKey:OMIT_DUPES];
}

/* - (void) observeValueForKeyPath: (NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	if ([keyPath isEqual:TRAINING_INTERVAL])
	{
		if (timer != nil)
		{
			[timer invalidate];
			[timer release];
			timer = nil;
			
			[self toggleTraining:nil];
		}
	}
} */

/* - (IBAction) toggleTraining:(id)sender
{
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	if (timer == nil)
	{
		self.trainingStatus = @"Training";
		self.trainingItemLabel = @"Stop Training";
		
		double interval = [[NSUserDefaults standardUserDefaults] doubleForKey:TRAINING_INTERVAL];
		if (interval < 5.0)
			interval = DEFAULT_INTERVAL;
		
		timer = [[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(train:) userInfo:nil repeats:YES] retain];
		
		[defaults setBool:NO forKey:OMIT_DUPES];
	}
	else
	{
		[timer invalidate];
		[timer release];
		timer = nil;
		
		self.trainingStatus = @"Not Training";
		self.trainingItemLabel = @"Start Training";

		[defaults setBool:YES forKey:OMIT_DUPES];
	}
} */

- (void) train:(NSTimer *) timer
{
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

	if (![defaults boolForKey:PREDICTIONS_ACTIVE])
		NSRunInformationalAlertPanel (@"Predictions Paused", @"Predictions are currently paused. Please resume predictions to allow the changes to take effect.", @"Ok", nil, nil);
	else
	{
		[defaults setBool:YES forKey:IS_TRAINING];

		// if ([defaults boolForKey:AUDIBLE_CUE])
		// 	[[NSSound soundNamed:@"bell"] play];

		[NSApp activateIgnoringOtherApps:YES];
		[predictionPanel makeKeyAndOrderFront:nil];
	}
}

- (IBAction) correctTraining:(id)sender
{
	[self train:nil];
}

@end
